#ifndef TLS_CAMERA_H
#define TLS_CAMERA_H

#include "vector.h"
#include "transform.h"
#include "film.h"

namespace tls{

  struct camera{
    mat4x4 m;
    real fov;//vertical fov
    real sizex,sizey;
    film f;

    real sx,sy;
    vec3 o,ori;

    explicit camera(const real& x=4,const real& y=3,const mat4x4& t=mat4x4(),
		    const real& fovy=pi/3,
		    int w=800,int h=600):
      sizex(x),sizey(y),m(t),fov(fovy),f(w,h){

      sx=sizex / f.width;
      sy=sizey / f.height;
      o=m.t().v() - m.x().v() * sizex * 0.5
	- m.y().v() * sizey * 0.5;
      real dist=1.0/std::tan(fov * 0.5) * sizey * 0.5;
      ori=m.t().v() + m.z().v() * dist;
    }

    //the 3D world position on the view plane
    vec3 get_pixel_center(int x,int y)const{
      return o + (2*x+1)*sx*0.5 * m.x().v()
	+ (2*y+1)*sy*0.5 * m.y().v();
    }
  };

  void* create_camera(FILE* f);
}//namespace tls

#endif
